<?php

namespace app\webapi\controller;

use app\webapi\common\BaseController;
use app\webapi\vms\service\CategoryService;
use app\webapi\vms\validate\CategoryValidate;
use think\App;

/**
 * @分类控制器
 */
class CategoryController extends BaseController
{
    /**
     * @var CategoryValidate
     */
    protected $validate;

    /**
     * @var CategoryService
     */
    protected $service;

    /**
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->validate = new CategoryValidate();
        $this->service = new CategoryService();
    }

    /**
     * @desc 获取分类树形列表
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/13-10:18
     */
    public function getTreeList()
    {
        $params = $this->validate->goCheck('getTreeList');
        $res = $this->service->getTreeList($params, $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 编辑分类
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/13-11:07
     */
    public function edit()
    {
        $params = $this->validate->goCheck('edit');
        $res = $this->service->edit($params, $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 删除
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/13-11:42
     */
    public function delete()
    {
        $params = $this->validate->goCheck('delete');
        $res = $this->service->delete($params['id'],$this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 排序分类
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/13-12:02
     */
    public function sort()
    {
        $params = $this->validate->goCheck('sort');
        $json_array= $this->validate->check_sort_json_array($params['json_array'],['id' => 'number', 'sort' => 'number']);
        $res = $this->service->sort($params['pid'],$json_array, $this->website_id);
        return $this->responseSuccess($res);
    }
}